﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using UtilityToolsCollect.ObjectsLibrary.Configs;

namespace SymbolicLinkManageCore
{
    [Serializable]
    public class Config : XmlConfigurationAbstract<Config>, INotifyPropertyChanged
    {
        private static void IfMainWindowCall(Action action)
        {
            if (ManageCore.MainWindow?.CheckAccess() ?? true)
                action?.Invoke();
            else
                ManageCore.MainWindow?.Dispatcher.Invoke(action, System.Windows.Threading.DispatcherPriority.DataBind);
        }
        public static bool IsEnableWriteLog => IsEnableWriteLog;
        public static void Add(RecordData Item) => IfMainWindowCall(() => Current.AddRecordData(Item));

        public static void Remove(RecordData Item) => IfMainWindowCall(() => Current.RemoveRecordData(Item));
        public static void Clear() => IfMainWindowCall(() => Current.ClearRecordData());
        //--------------------------------------------------------------------
        //--------------------------------------------------------------------
        public event PropertyChangedEventHandler? PropertyChanged;
        public void SendNotify(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        //--------------------------------------------------------------------
        //--------------------------------------------------------------------
        public bool EnableWriteLog { get => _EnableWriteLog; set { if (_EnableWriteLog == value) return; _EnableWriteLog = value; SendNotify(nameof(EnableWriteLog)); } }
        private ObservableCollection<RecordData> _RecordDatas = new();
        public ObservableCollection<RecordData> RecordDatas { get => _RecordDatas; set { if (_RecordDatas == value) return; _RecordDatas = value; SendNotify(nameof(RecordDatas)); } }

        private bool _EnableWriteLog = false;
        
        //--------------------------------------------------------------------
        //--------------------------------------------------------------------
        public void AddRecordData(RecordData Item) => RecordDatas.Add(Item);
        public void RemoveRecordData(RecordData Item) => RecordDatas.Remove(Item);
        public void ClearRecordData()=>RecordDatas.Clear();
        public override void Sort() => RecordDatas = new ObservableCollection<RecordData>(RecordDatas.OrderBy(p => p.LinkPointPath).ToArray());
        public override void Loaded()=> RecordDatas.ForEach(p => p.CheckAndClean());
    }
}
